package com.example.video.controller;

import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.io.RandomAccessFile;

/**
 * @ClassName HelloController
 * @Description
 * @Author lichuanqi
 * @Date 2022/9/2 16:53
 * @Version 1.0
 **/


@RestController
@RequestMapping("/hello")
public class HelloController {
    @GetMapping("play")
    public void play(HttpServletRequest request, HttpServletResponse response) throws IOException {
        /*
        GET /hello/play HTTP/1.1
        Accept-Encoding: identity;q=1, *;q=0
        Accept-Language: zh-CN,zh;q=0.9
        Connection: keep-alive
        Cookie: Idea-1316eb6b=a6fcd781-fc67-4d22-acf1-c2d65c28ce62
        Host: localhost:8080
        Range: bytes=18251776-
        Referer: http://localhost:63342/
        Sec-Fetch-Dest: video
        Sec-Fetch-Mode: no-cors
        Sec-Fetch-Site: same-site
        User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36
        sec-ch-ua: "Chromium";v="104", " Not A;Brand";v="99", "Google Chrome";v="104"
        sec-ch-ua-mobile: ?0
        sec-ch-ua-platform: "Windows"
        */

        /**
         *         html video标签分段加载视频播放的http请求包
         *         header中Range表示当前需要获取的数据在视频文件中的偏移量
         *         bytes=18251776- 表示当前请求要从文件的第18251776个字节开始读取
         */

        response.reset();
        File file = new File("E:/FFOutput/test.mp4");
        long fileLength = file.length();
        // 随机读文件
        RandomAccessFile randomAccessFile = new RandomAccessFile(file, "r");

        //获取从那个字节开始读取文件
        String rangeString = request.getHeader("Range");
        System.out.println("Range:"+rangeString);
        long range=0;
        //请求头格式Range:bytes=1048576-
        if (StringUtils.isNotBlank(rangeString)) {
            range = Long.valueOf(rangeString.substring(rangeString.indexOf("=") + 1, rangeString.indexOf("-")));
        }
        //获取响应的输出流
        OutputStream outputStream = response.getOutputStream();
        //设置内容类型
        response.setHeader("Content-Type", "video/mp4");
        //返回码需要为206，代表只处理了部分请求，响应了部分数据
        response.setStatus(HttpServletResponse.SC_PARTIAL_CONTENT);

        // 移动访问指针到指定位置
        randomAccessFile.seek(range);
        // 每次请求只返回1MB的视频流
        byte[] bytes = new byte[1024 * 1024];
        int len = randomAccessFile.read(bytes);
        //设置此次相应返回的数据长度
        response.setContentLength(len);
        //设置此次相应返回的数据范围
        response.setHeader("Content-Range", "bytes "+range+"-"+(fileLength-1)+"/"+fileLength);
        // 将这1MB的视频流响应给客户端
        outputStream.write(bytes, 0, len);
        outputStream.close();
        randomAccessFile.close();

        System.out.println("返回数据区间:【"+range+"-"+(range+len)+"】");
    }
}
